package com.ht.medicaltreatment.activity

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.InventoryQueryAdapter
import com.ht.medicaltreatment.bean.PDQueryBoxBean
import com.ht.medicaltreatment.viewmodel.BuildOrderViewModel
import com.xgst.lib.utils.ActivityLifecycleManager.finish
import kotlinx.android.synthetic.main.inventory_query_layout.btnAdd
import kotlinx.android.synthetic.main.inventory_query_layout.cbAll
import kotlinx.android.synthetic.main.inventory_query_layout.recyclerView
import kotlinx.android.synthetic.main.inventory_query_layout.tvCheckCount

class InventoryQueryActivity : BaseActivity(), OnItemClickListener {
    override fun getLayoutId() = R.layout.inventory_query_layout

    private val model by viewModels<BuildOrderViewModel>()
    private lateinit var itemAdapter: InventoryQueryAdapter
    override fun init() {
        setTitle("查询内容")
        val projectId = intent.getIntExtra("projectId",0)
        val sampleBoxName = intent.getStringExtra("sampleBoxName")
        val sampleCategoryId = intent.getStringExtra("sampleCategoryId")

        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = InventoryQueryAdapter().apply { itemAdapter = this }

        initClick()
        showProgress()
        model.findSample("$projectId",sampleBoxName,sampleCategoryId,{ list ->
            if(list?.size == 0){
                return@findSample
            }
            /*list?.let { alreadyList ->
                for (a in alreadyList.count() - 1 downTo 0){
                    sc@ for (i in list.count() - 1 downTo 0){
                        if(alreadyList[a].id == list[i].id){
                            alreadyList.removeAt(a)
                            list.removeAt(i)
                            break@sc
                        }
                    }
                }
            }*/
            itemAdapter.setNewInstance(list)
            dismissProgress()
        })
    }

    private fun initClick() {
        //添加
        btnAdd.setOnClickListener {
            val list = itemAdapter.data.filter { it.isChecked }.apply { forEach { it.isChecked = false } }
            val json = GsonUtils.toJson(list)
            val intent = Intent()
            intent.putExtra("json",json)
            setResult(RESULT_OK,intent)
            finish()
        }
        //全选
        cbAll.setOnClickListener{
            itemAdapter.data.forEach {
                it.isChecked = cbAll.isChecked
            }
            if(cbAll.isChecked){
                tvCheckCount.text = "${itemAdapter.data.size}"
            }else{
                tvCheckCount.text = "0"
            }
            itemAdapter.notifyDataSetChanged()
        }

        itemAdapter.setOnItemClickListener(this)
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as PDQueryBoxBean
        item.isChecked = !item.isChecked
        adapter.notifyItemChanged(position)
        val list = adapter.data as MutableList<PDQueryBoxBean>
        val bean = list.find { !it.isChecked }
        if(bean == null){
            cbAll.isChecked = true
        }else{
            if(cbAll.isChecked){
                cbAll.isChecked = false
            }
        }
        tvCheckCount.text = "${list.filter { it.isChecked }.size}"
    }
}
